/*
    Copyright (C) 2025 mio <stigma@disroot.org>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
#include "playlist_window.h"

#include "playlist.h"

#include <tqlayout.h>
#include <tqpalette.h>
#include <tqtable.h>
#include <tqpainter.h>

#include <tdefilemetainfo.h>
#include <tdelocale.h>
#include <tdemenubar.h>

class PlaylistTable : public TQTable
{
public:
	PlaylistTable(TQWidget *parent, const char *name)
		: TQTable(parent, name)
		, m_currentRow(0)
	{}

	void paintCell(TQPainter *p, int row, int col, const TQRect& cr, bool selected, const TQColorGroup& cg) override
	{
		if (row == m_currentRow)
		{
			// TODO: This should be configurable via settings.
			TQColorGroup group = cg;

			group.setColor(TQColorGroup::Text, palette().active().buttonText());
			group.setBrush(TQColorGroup::Base, palette().active().button());

			TQTable::paintCell(p, row, col, cr, selected, group);
		}
		else
		{
			TQTable::paintCell(p, row, col, cr, selected, cg);
		}
	}

	void setCurrentRow(int row)
	{
		m_currentRow = row;
		repaintContents(visibleRect());
	}

private:
	int m_currentRow;
};

PlaylistWindow::PlaylistWindow(Playlist *playlist, TQWidget *parent, const char *name)
	: TQWidget(parent, name, WType_TopLevel)
	, m_playlist(playlist)
{
	setCaption(i18n("Aster - Playlist"));

	auto layout = new TQVBoxLayout(this);

    m_menuBar = new KMenuBar(this, "playlist_window_menubar");
    m_menuBar->insertItem("Open File...", 0); // TODO: Should this just emit a signal?
    m_menuBar->insertItem("Remove Item", this, TQ_SLOT(slotRemoveCurrent()), Key_Delete, 1);
    m_menuBar->setItemEnabled(1, m_playlist->count() > 1);

	m_table = new PlaylistTable(this, "playlist_window_table");
	m_table->setFocusStyle(TQTable::FollowStyle);
	m_table->setLeftMargin(0);
	m_table->setNumCols(2);
	m_table->setReadOnly(true);
	m_table->setSelectionMode(TQTable::SingleRow);

	m_table->horizontalHeader()->setLabel(0, i18n("Title"));
	m_table->horizontalHeader()->setLabel(1, i18n("Duration"));
	m_table->setColumnStretchable(0, true);

	connect(m_playlist, TQ_SIGNAL(currentIndexChanged(int)), TQ_SLOT(slotIndexChanged(int)));
	connect(m_playlist, TQ_SIGNAL(itemsInserted(int, int)),	TQ_SLOT(slotItemsInserted(int, int)));
    connect(m_playlist, TQ_SIGNAL(itemsRemoved(int, int)), TQ_SLOT(slotItemsRemoved(int, int)));

	connect(m_table, TQ_SIGNAL(doubleClicked(int, int, int, const TQPoint&)),
		TQ_SLOT(slotTableDoubleClicked(int, int, int, const TQPoint&)));

    layout->add(m_menuBar);
	layout->add(m_table);

	/* FIXME: No way to iterate playlist */
	m_table->insertRows(0, m_playlist->count());
	for (size_t i = 0; i < m_playlist->count(); ++i)
	{
		KFileMetaInfo info(m_playlist->at(i));
		if (info.isEmpty())
		{
			m_table->setText(i, 0, m_playlist->at(i).fileName());
			m_table->setText(i, 1, TQString::fromLatin1("00:00:00"));
		}
		else
		{
			m_table->setText(i, 0, "TITLE!");
			m_table->setText(i, 1, TQString::fromLatin1("00:00:00"));
		}
	}

	static_cast<PlaylistTable*>(m_table)->setCurrentRow(m_playlist->currentIndex());

	resize(500, 200);
	move(parent->x() + (parent->width() / 2) - (width() / 2),
         parent->y() + (parent->height() / 2) - (height() / 2));
}

void PlaylistWindow::keyReleaseEvent(TQKeyEvent *event)
{
	switch (event->key())
	{
	case Key_Enter:
	case Key_Return: {
		int current = m_table->selection(m_table->currentSelection()).topRow();
		if (current == m_playlist->currentIndex()) {
			// Should this restart? (as with double-clicking)
			return;
		}
		m_playlist->setCurrentIndex(current);
		break;
	}
	default:
		break;
	}
}

void PlaylistWindow::slotIndexChanged(int index)
{
	static_cast<PlaylistTable*>(m_table)->setCurrentRow(index);
}

void PlaylistWindow::slotItemsInserted(int start, int end)
{
	const int count = end - start;
	m_table->insertRows(m_table->numRows(), count);
	for (int i = 0; i < count; ++i)
	{
		KFileMetaInfo info(m_playlist->at(start + i));
		if (info.isEmpty())
		{
			m_table->setText(start + i, 0, m_playlist->at(start + i).fileName());
			m_table->setText(start + i, 1, TQString::fromLatin1("00:00:00"));
		}
		else
		{
			m_table->setText(start + i, 0, "TITLE!");
			m_table->setText(start + i, 1, TQString::fromLatin1("00:00:00"));
		}
	}

    // Ensure the 'remove item' is active.
    m_menuBar->setItemEnabled(1, true);
}

void PlaylistWindow::slotItemsRemoved(int start, int end)
{
    m_table->removeRow(start);
    if (m_playlist->count() == 0)
        m_menuBar->setItemEnabled(1, false);
    tqWarning("PlaylistWindow::slotItemsRemoved: implementation isn't finished as multiple selections are not supported.");
}

void PlaylistWindow::slotRemoveCurrent()
{
    m_playlist->remove(m_table->currentRow());
}

void PlaylistWindow::slotTableDoubleClicked(int row, int, int button, const TQPoint&)
{
	if (button != TQt::LeftButton)
	{
		return;
	}

	// Should this restart?
	if (m_playlist->currentIndex() == row)
	{
		return;
	}

	m_playlist->setCurrentIndex(row);
}

#include "playlist_window.moc"
